package run.fenglu.jls.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.google.gson.reflect.TypeToken;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;

import java.util.Date;

@Configuration
public class GsonConfig {

    @Bean
    GsonHttpMessageConverter gsonHttpMessageConverter(){
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        converter.setGson(gson());
        return converter;
    }


    @Bean
    Gson gson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
//        gsonBuilder.setPrettyPrinting();
        gsonBuilder.registerTypeAdapter(new TypeToken<Date>(){}.getType(), new DateTypeAdapter());
//        gsonBuilder.registerTypeAdapter(new TypeToken<BigDecimal>(){}.getType(), new BigDecimalTypeAdapter());
//        gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
//        gsonBuilder.setDateFormat(DateFormat.LONG);
        return gsonBuilder.create();
    }
}
